package com.jyh.supermark.service.impl;

import com.jyh.domain.User;
import com.jyh.supermark.dao.impl.GoodsDaoImpl;
import com.jyh.supermark.domain.BuyCar;
import com.jyh.supermark.domain.Goods;
import com.jyh.supermark.service.GoodsService;
import com.jyh.utils.BaseDAO;
import org.junit.Test;

import java.util.LinkedList;
import java.util.List;
import java.util.Scanner;

public class GoodsServiceImpl implements GoodsService {
    public static final Scanner INPUT = new Scanner(System.in);
    public static final GoodsDaoImpl goodsDaoImpl = new GoodsDaoImpl();

    public void findAll() {
        List<Goods> goodsList = BaseDAO.selectMultipleRow("select * from goods", Goods.class);
        System.out.println("商品编号\t\t商品名称\t\t商品价格\t\t商品库存");
        for (Goods goods : goodsList) {
            System.out.println(goods.getId() + "\t\t" + goods.getName() + "\t\t" + goods.getPrice() + "\t\t" + goods.getStock());
        }
    }

    @Override
    public void shopping() {
        int sumNum = 0;
        outer:
        while (true) {
            findAll();
            System.out.println("请输入商品编号：");
            String id = INPUT.next();
            List<Goods> goodsList = goodsDaoImpl.selectByGoodsId(id);
            if (goodsList.size() == 0) {
                System.out.println("您输入的商品编号不存在！");
                continue;
            }
            for (Goods goods : goodsList) {
                if (goods.getStock() <= 0) {
                    goods.setStock(0);
                    System.out.println("库存为0，无法购买，等待补货");
                    continue;
                }
                while (true) {
                    System.out.println("请输入购买数量：");
                    Integer buyNum = INPUT.nextInt();
                    if (buyNum <= 0) {
                        System.out.println("购买数量不能为零或者负数");
                        continue;
                    }
                    if (buyNum > goods.getStock()) {
                        System.out.println("购买数量必须小于" + goods.getStock());
                        continue;
                    }
                    else {
                        System.out.println("您还要继续购买吗？(是y 否任意键)");
                        INPUT.nextLine();
                        String choice = INPUT.nextLine();
                        switch (choice) {
                            case "y":
                                BuyCar buyCar = new BuyCar(goods.getId(), goods.getName(), goods.getPrice(), buyNum);
                                goodsDaoImpl.insertBuyCar(buyCar);
                                int count = goodsDaoImpl.updateGoodsStockInfo(buyCar.getId(), goods.getStock() - buyNum);
                                if (count > 0) {
                                    System.out.println("库存修改成功！");
                                }
                                continue outer;
                            default:
                                BuyCar buyCar1 = new BuyCar(goods.getId(), goods.getName(), goods.getPrice(), buyNum);
                                goodsDaoImpl.insertBuyCar(buyCar1);
                                int count1 = goodsDaoImpl.updateGoodsStockInfo(buyCar1.getId(),goods.getStock()-buyNum);
                                if (count1 > 0) {
                                    System.out.println("库存修改成功！");
                                }
                                return;
                        }
                    }
                }
            }
        }
    }
}
